package com.hx.vending.service.impl;

import com.hx.vending.bean.SubsidiaryRole;
import com.hx.vending.bean.User;
import com.hx.vending.bean.UserOperation;
import com.hx.vending.bean.entity.SuperOperation;
import com.hx.vending.dao.SubsidiaryRoleMapper;
import com.hx.vending.dao.UserMapper;
import com.hx.vending.dao.UserOperationMapper;
import com.hx.vending.service.ISubsidiaryRoleService;
import com.hx.vending.util.DateUtil;
import com.hx.vending.util.Msg;
import com.hx.vending.util.ToolUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/3/12.
 */
@Service
public class SubsidiaryRoleServiceImpl implements ISubsidiaryRoleService {

    @Resource
    private SubsidiaryRoleMapper subsidiaryRoleMapper;
    @Resource
    private UserOperationMapper userOperationMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public Msg addSubsidiaryRole(SubsidiaryRole subsidiaryRole,Integer userId) {
        subsidiaryRole.setSubsidiaryRoleUserId(userId);
        subsidiaryRole.setSubsidiaryRoleCreatetime(DateUtil.getTime());
        int i = subsidiaryRoleMapper.insertSelective(subsidiaryRole);
        if (i > 0){
            return Msg.success();
        }
        return Msg.fail();
    }

    @Override
    public Msg updateSubsidiaryRole(SubsidiaryRole subsidiaryRole) {
        int i = subsidiaryRoleMapper.updateByPrimaryKey(subsidiaryRole);
        if(i > 0){
            return Msg.success();
        }
        return Msg.fail();
    }

    @Override
    public Msg selectSubsidiaryRole(Integer userId) {
        List<SubsidiaryRole> subsidiaryRoleList = subsidiaryRoleMapper.selectByUserId(userId);
        return Msg.success().add("roleList",subsidiaryRoleList);
    }

    @Override
    public Msg deleteSubsidiaryRole(String roleIds) {
        Integer i = 0;
        if(roleIds.contains("-")){
            String[] strs = roleIds.split("-");
            List<User> userList = userMapper.selectByRoleIds(strs);
            if (userList.size() > 0){
                return Msg.success().add("msg","该岗位正在使用");
            }
            i = subsidiaryRoleMapper.deleteByBatch(strs);
        }else {
            List<User> userList = userMapper.selectByRoleId(Integer.parseInt(roleIds));
            if (userList.size() > 0){
                return Msg.success().add("msg","该岗位正在使用");
            }
            i = subsidiaryRoleMapper.deleteByPrimaryKey(Integer.parseInt(roleIds));
        }
        if(i > 0){
            return Msg.success();
        }
        return Msg.fail();
    }

    @Override
    public Msg selectRoleByRoleId(Integer subsidiaryRoleId) {
        SubsidiaryRole s = new SubsidiaryRole();
        s.setSubsidiaryRoleId(subsidiaryRoleId);
        SubsidiaryRole selectSubsidiaryRole = subsidiaryRoleMapper.selectByPrimaryKeySelective(s);
        return Msg.success().add("selectSubsidiaryRole", selectSubsidiaryRole);
    }
}
